PROGRAM	TabletGrabber;
{$R TabletGrabber.Rsrc}
{$U-}

USES
    Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf;
    
TYPE

    OpData = record
    TabltID : longint;
    R0Data : array [0..9] of signedbyte;
    MyFlags : byte;
    R1Data : array [0..9] of signedbyte;
    ResrvdByte : byte;
    ResrvdPtr : ptr;
    end;
    
    OpDataPtr = ^OpData;
    
    SysEnvRec	  =	   RECORD
				environsVersion:	INTEGER;
				machineType:		   INTEGER;
				systemVersion:		 INTEGER;
				processor:		    	INTEGER;
				hasFPU:				      BOOLEAN;
				hasColorQD:	   		BOOLEAN;
				keyBoardType:  		INTEGER;
				atDrvrVersNum: 		INTEGER;
				sysVRefNum:		   	INTEGER;
				END;

    SysEnvRecPtr = ^SysEnvRec;
    
    longintptr = ^longint;


CONST
      TextStatItem4 = 4;   {Item number of static text}
      TextStatItem5 = 5;   {Item number of static text}
      textItem = 3;        {item number for editable text item}
      errorDialID = 257;
      MainDialId = 258;
      TabltAddrs = 4;
      ID = $54424C54;

VAR
    err : integer;
	 		DialRect : Rect;
	 		DialPtr : DialogPtr;
	 		DialItemHit, DITLID,i : integer;
	 		DITLHndl : handle;
    GotADB,quit :boolean;
    ResHell,theType : integer;   {gives the type of the item requested}
    TextHdl1,TextHdl2 : handle;    {gives a handle to the item}
    txtBox : Rect;    {gives the display rectangle of the item}
    IDVal : packed array [0..4] of char;
    OldX,OldY : array [0..1] of signedbyte;
    MyBuffer,LongPtr : longintptr;
    Environment : SysEnvRec;
    EnvPtr : SysEnvRecPtr;
    InfoBlk : ADBDataBlock;
    DataAreaPtr : OpDataPtr;
    theEvent : EventRecord;

		{------------------------------------------------------------------------------------}
	
  PROCEDURE debugger; INLINE $A9FF;

		{------------------------------------------------------------------------------------}

  FUNCTION SysEnvirons(versionRequested: INTEGER; VAR theWorld: SysEnvRec): OSErr;
  
  INLINE $205f,$301f, $A090, $3e80;

		{------------------------------------------------------------------------------------}

		PROCEDURE InitMac;

			BEGIN					   {InitMac}

   InitGraf (@thePort);          {the big five inits + 1}
   InitFonts;
   InitWindows;
   TEInit;
   InitDialogs (nil);
   InitCursor;
			END;					   {InitMac}

		{------------------------------------------------------------------------------------}

PROCEDURE DoError (ErStr : str255; err : longint);

CONST
    TextStatItem = 2;
VAR
    NumStr : str255;
    theTextHdl : handle;
	 		DialPtr : DialogPtr;
	 		DialItemHit, DITLID : integer;

    Begin
		if err <> 0 then NumToString (err,NumStr)
		else NumStr := '';
        ErStr := Concat (ErStr,NumStr);
        DITLID := 257;
        DITLHndl := GetResource ('DITL', DITLID);
        ResHell := ResError;
        if ResHell = noerr then
        begin
        	if DITLHndl <> nil then

        	begin
            	Hlock (DITLHndl);

            	DialPtr := GetNewDialog (257,nil,WindowPtr(-1));
            	If DialPtr <> nil then
            	begin
            		GetDItem (DialPtr, TextStatItem, theType, theTextHdl, txtBox);
            		If theTextHdl <> nil then
            		begin
     
            			SetPort (DialPtr);

            			SetIText (theTextHdl, ErStr);
           				repeat
            				ModalDialog (nil, DialItemHit);
            			until DialItemHit <> 0;
            			Case DialItemHit of  {1 = OK btn, 2 = message}
                		1,2 : CloseDialog (DialPtr);
								
			  	    	end; {case}
            		end;
            	end;
        	end;
        end;
		If DITLHndl <> nil then HUnLock (DITLHndl);
     end;

		{------------------------------------------------------------------------------------}

PROCEDURE ChangeIt (FirstOne, SecondOne:integer);

VAR
    FirstOneTxt,SecondOneTxt : str255;
    
begin
    NumToString (longint(FirstOne), FirstOneTxt);
    NumToString (longint(SecondOne), SecondOneTxt);
    SetIText (TextHdl1, FirstOneTxt);
    SetIText (TextHdl2, SecondOneTxt);
end;
    
		{------------------------------------------------------------------------------------}

	
    begin {main DudeFace}
      
        initmac;
        GotADB := False;
        quit := false;
        OldX[0] := 0;
        OldX[1] := 0;
        OldY[0] := 0;
        OldY[1] := 0;
        err := SysEnvirons(1,Environment);
        if err = noerr then
        begin
            case Environment.machineType of
            3,4 : GotADB := true;
            end; {case}
            if GotADB then
            begin
                err := GetADBInfo (InfoBlk,TabltAddrs);
                if err = noerr then
                begin
                    if infoBlk.dbDataAreaAddr <> nil then
                    begin
                        DataAreaPtr :=  OpDataPtr(infoBlk.dbDataAreaAddr);
                        if DataAreaPtr^.TabltID = ID then
                        begin
                            DITLID := MainDialId;
                            DITLHndl := GetResource ('DITL', DITLID);
                            ResHell := ResError;
                            if ResHell = noerr then
                            begin
                                if DITLHndl <> nil then
                                begin
                                    Hlock (DITLHndl);
                                    SetRect(DialRect,310,200,screenbits.bounds.right - 210,
                                            screenbits.bounds.bottom - 100);			
	                                   DialPtr := GetNewDialog (MainDialId,nil,WindowPtr(-1));
                                    If DialPtr <> nil then
                                    begin
                                        SetPort (DialPtr);
                                        GetDItem (DialPtr, TextStatItem4, theType, TextHdl1, txtBox);
                                        If TextHdl1 <> nil then
                                        begin
                                            GetDItem (DialPtr, TextStatItem5, theType, TextHdl2, txtBox);
                                            If TextHdl2 <> nil then repeat
                                            begin
                                                if (OldX[0] <> DataAreaPtr^.R0Data[3]) or
                                                   (OldX[1] <> DataAreaPtr^.R0Data[4]) or
                                                   (OldY[0] <> DataAreaPtr^.R0Data[5]) or
                                                   (OldY[1] <> DataAreaPtr^.R0Data[6]) then
                                                begin
                                                    OldX[0] := DataAreaPtr^.R0Data[3];
                                                    OldX[1] := DataAreaPtr^.R0Data[4];
                                                    OldY[0] := DataAreaPtr^.R0Data[5];
                                                    OldY[1] := DataAreaPtr^.R0Data[6];
                                                    ChangeIt (integer(OldX), integer(OldY));
                                                end;
                                                if GetNextEvent (EveryEvent, theEvent) then
                                                begin
                                                    if IsDialogEvent (theEvent) then
                                                    begin
                                                        if DialogSelect (theEvent, DialPtr, DialItemHit) then
                                                        case DialItemHit of
                                                        1 : quit := true;
                                                        end {case}
                                                    end;
                                                end;
                                            end;
                                            until quit;
                                        end;
                                    end
                                    else DoError ('Dialog Pointer is nil',0);
                                end
                                else DoError ('DITL Handle is nil',0);
                            end
                            else DoError ('Resource Error (DITL)',ResHell);
                        end
                        else DoError ('Can''t find ''TBLT'' ID',0);
                    end
                    else DoError ('Can''t find tablet data area',0);
                end
                else DoError ('GetADBInfo Error',err);
            end
            else DoError ('Yes, we have no ADB today',0);
        end
        else DoError ('SysEnvirons Error',err);
    end.